﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class MistakeDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Mistake.GetAll);
		}

		public static DataTable GetOne(string mistakeID)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@MistakeID", SqlDbType.NVarChar, mistakeID)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Mistake.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static Mistake LoadOne(string mistakeID)
		{
			Mistake o = new Mistake();
			DataTable dt = GetOne(mistakeID);
			if(dt.Rows.Count > 0)
			{
				o.MistakeID = dt.Rows[0][ToolConstants.Entities.Mistake.FieldName.MistakeID].ToString();
				o.MistakeEnglish = dt.Rows[0][ToolConstants.Entities.Mistake.FieldName.MistakeEnglish].ToString();
				o.MistakeJanpanese = dt.Rows[0][ToolConstants.Entities.Mistake.FieldName.MistakeJanpanese].ToString();

			}

			return o;
		}


		public static int Insert(Mistake o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@MistakeID", SqlDbType.NVarChar, o.MistakeID),
                        CreateParameter("@MistakeEnglish", SqlDbType.NVarChar, o.MistakeEnglish),
                        CreateParameter("@MistakeJanpanese", SqlDbType.NVarChar, o.MistakeJanpanese),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Mistake.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new MyException(e);
			}

		}

		public static int Update(Mistake o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@TempID", SqlDbType.NVarChar, o.TempId),
                        CreateParameter("@MistakeID", SqlDbType.NVarChar, o.MistakeID),
                        CreateParameter("@MistakeEnglish", SqlDbType.NVarChar, o.MistakeEnglish),
                        CreateParameter("@MistakeJanpanese", SqlDbType.NVarChar, o.MistakeJanpanese),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Mistake.Update, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static int Delete(Mistake o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@MistakeID", SqlDbType.NVarChar, o.MistakeID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Mistake.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_Mistake");

			return table;
		}

		#endregion
	}
}
